home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11584 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.7 KB  |  185 lines

  1. Path: prodigy.com!usenet
  2. From: EWTE97A@prodigy.com (Gregory Gibson)
  3. Newsgroups: comp.lang.c++
  4. Subject: BC4.5 won't compile it; GNU will [templates]
  5. Date: 15 Mar 1996 05:48:36 GMT
  6. Organization: Prodigy Services Company  1-800-PRODIGY
  7. Distribution: world
  8. Message-ID: <4ib0bk$16cg@usenetz1.news.prodigy.com>
  9. NNTP-Posting-Host: innugap2-int.news.prodigy.com
  10. X-Newsreader: Version 1.2
  11.  
  12. //GNU compiles this and it runs.  BC 4.5 does not compile it.
  13. //And simple errors like (a<>b) instead of (a!=b) are ignored? 
  14. //What is wrong with BC 4.5?
  15.  
  16. // I APPRECIATE THE TIME INVESTMENT YOU MAKE!!!
  17.  
  18. // BC 4.5 does not seem to recognize the global 'pool'
  19.  
  20. //=================================================================
  21. #include <iostream.h>
  22. #include <stdio.h>
  23. #include <string.h> /* strcpy prototype */
  24.  
  25. #define TSIZE 45
  26. typedef char * string;
  27.  
  28. //==================================================================
  29. class film {  // I put this here because everything depends on it.
  30.           char title[TSIZE];
  31.   public:
  32.           film()                  { strcpy(title,""); }
  33.           film(string s)          { strcpy(title,s); }
  34.           operator string()       { return title; }
  35.           friend ostream& operator<< (ostream& out, film& z);
  36. };
  37.  
  38. ostream& operator<< (ostream& out, film& z) { return (out << z.title); }
  39.  
  40. //==================================================================
  41. //CIRCULAR SORTED LINKED LIST CLASS TEMPLATE
  42. template <class GenTy>
  43. class list {
  44.           GenTy data;
  45.   public:
  46.           list *next;
  47.  
  48.           list(string s="zzzzzzzzzz") {
  49.                      data = s;
  50.                      next = this;
  51.           }
  52.  
  53.           GenTy getdata() { return data; }
  54.  
  55.           list<GenTy>* getptr(string s){
  56.                      list<GenTy>* temp;
  57.                      temp=pool.new_node();
  58.                      temp->data=s;
  59.                      return temp;
  60. }
  61.  
  62.   int empty() {return this==this->next;}
  63.  
  64.   void find(list<GenTy>* &, list<GenTy>* &,const string &);
  65.  
  66.   void add(const string &);
  67.  
  68.   void del(const string &);
  69.  
  70.   void print();
  71. };
  72.  
  73. template <class GenTy> //FIND
  74. void list<GenTy>::find(list<GenTy>* & p, list<GenTy>* & c,const string & 
  75. s) {
  76.           while (strcmp(c->data,s) < 0) { p=c; c=c->next;}
  77. }
  78.  
  79. template <class GenTy> //ADD
  80. void list<GenTy>::add(const string & s) {
  81.           cout << "adding: " << s << endl;
  82.           list<GenTy> *newptr=getptr(s), *p=this, *c=this->next;
  83.           list<GenTy>::find(p,c,s);
  84.           newptr->next=p->next;
  85.           p->next=newptr;
  86. }
  87.  
  88. template <class GenTy> //DEL
  89. void list<GenTy>::del(const string & s) {
  90.           cout << "attempting to delete: " << s << endl;
  91.           list<GenTy> *p=this, *c=this->next;
  92.           list<GenTy>::find(p,c,s);
  93.           if (strcmp(c->data,s) != 0)
  94.           cout << s << " not in list." << endl;
  95.           else {
  96.                      p->next=c->next;
  97.                      pool.recycle(c);
  98.           }
  99. }
  100.  
  101. template <class GenTy> //PRINT
  102. void list<GenTy>::print() {
  103.           if (list<GenTy>::empty()) cout << "Empty list! " << endl;
  104.           else {
  105.                      list<GenTy> *cur=this;
  106.                      while (cur->next!=this) {
  107.                                 cur=cur->next;
  108.                                 cout << cur->data << endl;
  109. }       }       }
  110.  //=======================================================
  111. // MEMORY MANAGER CLASS TEMPLATE (PARTIAL)
  112.  
  113. template <class N>   // I changed N to be the same type used to
  114.                             // instantiate list.
  115. class memman {
  116.           typedef list<N> node;
  117.           node *pool_next, *pool_end, *r_bin;
  118.  
  119.           void fill_pool(int i) {              // added void
  120.                      pool_next = new node[i];
  121.                      pool_end = pool_next+(i-1);
  122.           }
  123.  
  124.  public:
  125.           memman()      { fill_pool(4); r_bin=NULL; }
  126.           memman(int i) { fill_pool(i); r_bin=NULL; }
  127.  
  128.           list<N> * new_node();    // return a node from r_bin or the pool
  129.           void recycle(list<N> *); // put deallocated node on head of list
  130.           void show_bin();         // prints contents of recycle bin
  131. };
  132.  
  133. template <class N>  //NEW_NODE
  134. list<N>* memman<N>::new_node() {
  135.           node * temp;
  136.           if (r_bin!=NULL) {
  137.                       temp=r_bin; r_bin=r_bin->next;
  138.                       return temp; }
  139.              else {
  140.                  if (pool_next==pool_end) fill_pool(4);
  141.                  temp=pool_next; pool_next+=1; return temp;}
  142. }
  143.  
  144. template <class N>  //RECYCLE
  145. void memman<N>::recycle(list<N>* divorced) {
  146.           divorced->next=r_bin;
  147.           r_bin=divorced;
  148. }
  149.  
  150. template <class N>  //SHOW_BIN
  151. void memman<N>::show_bin() {
  152.           node * temp=r_bin;
  153.           while (temp != NULL) {
  154.                 cout << temp->data << endl;
  155.                 temp=temp->next;}
  156. }
  157.  
  158. //===========================================================
  159.  
  160. memman<film> pool;  // This is the key to making everything work together.
  161.  
  162.  
  163. void main() {
  164.  
  165.           list<film> movies;
  166.           char input[TSIZE];
  167.  
  168.           puts("Enter first movie title:");
  169.           while (gets(input) != NULL && input[0] != '\0') {
  170.  
  171.           if (input[strlen(input)-1] == '-') {    // if last character is
  172.                                                                 //  "-" delete
  173.           input[strlen(input)-1]='\0';    //last char is "-" remove it
  174.           movies.del(input);
  175.           }
  176.      else movies.add(input);
  177.           puts("Enter next movie title (`title-' deletes; linefeed to stop):"); 
  178. }
  179.           movies.print();
  180. }
  181.  
  182.  
  183.  
  184.  
  185.